define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
                chartDefault:{
                    chartType:"line",
                    titles:[
                        {id:"pv","name":"展示数"},{id:"click","name":"点击数"},{id:"price","name":"点击均价"},{id:"cost","name":"花费"},{id:"cr","name":"点击率"}
                    ]
                },
                chartQueue:{count:2,list:["pv","click"]}
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.report.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        return $.ExtCommon.getMappingJson(info,{
                            id:"id", name:"companyName",pv:"pv",click:"click",cr:"cr",price:"cpc",cost:"cost"
                        },true);
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getListCity:function (param) {
            var paramData=this._convertParam(param);
            servers.report.reportCity(paramData,this._reportDetailCallback("city",param));
        },
        getListGender:function (param) {
            var paramData=this._convertParam(param);
            servers.report.reportGender(paramData,this._reportDetailCallback("gender",param));
        },
        getListAge:function (param) {
            var paramData=this._convertParam(param);
            servers.report.reportAge(paramData,this._reportDetailCallback("age",param));
        },
        _convertParam:function(param){
            return  $.ExtCommon.getMappingJson(param, {
                startDate:"startTime",endDate:"endTime",advertiserId:"customerId",campaignId:"campaignId",strategyId:"strategyId"
            });
        },
        _reportDetailCallback: function (type,param,trigger) {
            var self = this;
            return function(res){
                var newJson={list:[],total:{}};
                if(res.success){
                    var list= $.ExtCommon.isCheckEmpty(res.data.list)?res.data:res.data.list;
                    var mapping={id:"id",description:"desc",pv:"pv",click:"click",cost:"cost", cr:"cr",price:"cpc"};
                    var mapEmptyZero=["pv","click","cost","cr","price"];
                    newJson.list=$.map(list,function (info) {
                        var newInfo=$.ExtCommon.getMappingJson(info,mapping,true);
                        $.ExtCommon.conversionEmptyZero(newInfo,mapEmptyZero);
                        return newInfo
                    });
                    newJson.total=$.ExtCommon.getMappingJson(res.data.total,mapping,true);
                    $.ExtCommon.conversionEmptyZero(newJson.total,mapEmptyZero);
                }
                res.dataOriginal=res.data;
                res.data=newJson;
                var req=$.ExtCommon.getJsonClone(param);
                req.type=type;
                if(trigger){
                    trigger({req:req,res:res})
                }else{
                    self.set({dataReportDetail:{req:req,res:res}});
                    self.trigger('bind:ReportDetail');
                }
            }
        },

        //订单报告
        getListHourByCampaign:function (param) {
            var self=this;
            var paramData=this._convertParam(param);
            servers.report.campaignHour(paramData,this._reportDetailCallback("hour",param,function (res) {
                self._triggerCampaign(res);
            }));
        },
        getListDayByCampaign:function (param) {
            var self=this;
            var paramData=this._convertParam(param);
            servers.report.campaignDay(paramData,this._reportDetailCallback("day",param,function (res) {
                self._triggerCampaign(res);
            }));
        },
        _triggerCampaign:function (res) {
            this.set({dataReportCampaign:res});
            this.trigger('bind:ReportCampaign');
        },

        //策略报告
        getListHourByStrategy:function (param) {
            var self=this;
            var paramData=this._convertParam(param);
            servers.report.strategyHour(paramData,this._reportDetailCallback("hour",param,function (res) {
                self._triggerStrategy(res);
            }));
        },
        getListDayByStrategy:function (param) {
            var self=this;
            var paramData=this._convertParam(param);
            servers.report.strategyDay(paramData,this._reportDetailCallback("day",param,function (res) {
                self._triggerStrategy(res);
            }));
        },
        _triggerStrategy:function (res) {
            this.set({dataReportStrategy:res});
            this.trigger('bind:ReportStrategy');
        },

        //down
        downloadExcelHour:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downHour(paramData)
        },
        downloadExcelDay:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downDay(paramData)
        },
        downloadStrategyGroup:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downloadStrategyGroup(paramData)
        },
        downloadStrategy:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downloadStrategy(paramData)
        },
        downloadCity:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downloadCity(paramData)
        },
        downloadGender:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downloadGender(paramData)
        },
        downloadAge:function(param){
            var paramData=this._convertParamExcel(param);
            servers.report.downloadAge(paramData)
        },
        _convertParamExcel:function(param){
            var paramData=this._convertParam(param);
            paramData.exportExcel=true;
            return $.ExtCommon.getUrlParamByJson(paramData)
        }
    });
});